﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;
using System.Data;
using SQLServer.DBUtility;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_SetPayWay 的摘要说明
    /// </summary>
    public class Hbung_SetPayWay : IHttpHandler
    {
        /// <summary>
        /// 设置支付方式
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //订单号
            string Hb_OrderNum = context.Request.Params["OrderNum"];
            if (string.IsNullOrEmpty(Hb_OrderNum))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("OrderNum为空"));
                return;
            }
            string OrderStr = string.Format("select * from OnlineOrders where OrderNum='{0}' and CPhone='{1}' ", Hb_OrderNum, Hbung_Phone);
            DataTable Mydata = DbLocalSQL.Query(OrderStr).Tables[0];
            if (Mydata == null || Mydata.Rows.Count ==0)
            {
                context.Response.Write(GenerClass.SetChangeErrorJsData("订单不存在"));
                return;
            }

            string StrWhere = string.Empty;
            //支付方式 0线上 1 线下
            string Hb_PayWay = context.Request.Params["Way"];
            if(string.IsNullOrEmpty(Hb_PayWay))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Way为空"));
                return;
            }
            else
            {
                if(Hb_PayWay!="0"&&Hb_PayWay!="1")
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Way格式不正确"));
                    return;
                }
                StrWhere += string.Format("PaymentMethod='{0}'",Hb_PayWay);
            }

            //租金优惠金额
            string Hb_YHPrice = context.Request.Params["Price"];
            decimal CPrice = 0m;
            if(Hb_PayWay=="0")
            {
                if (string.IsNullOrEmpty(Hb_YHPrice))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Price为空"));
                    return;
                }
                else
                {
                    if (!decimal.TryParse(Hb_YHPrice, out CPrice))
                    {
                        context.Response.Write(GenerClass.SetParamErrorJsData("Price格式不正确"));
                        return;
                    }
                    StrWhere += string.Format(",FavorableForOnline='{0}'", CPrice);
                }
            }

            //待支付金额
            string Hb_SumPrice = context.Request.Params["SPrice"];
            decimal sprice = 0m;
            if (Hb_PayWay == "0")
            {
                if (string.IsNullOrEmpty(Hb_SumPrice))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("SPrice为空"));
                    return;
                }
                else
                {
                    if(!decimal.TryParse(Hb_SumPrice,out sprice ))
                    {
                        context.Response.Write(GenerClass.SetParamErrorJsData("SPrice格式不正确"));
                        return;
                    }
                    StrWhere += string.Format(",ExecutoryCost='{0}'", sprice);
                }
            }
            string strSQl = string.Format("update OnlineOrders set {0} where OrderNum='{1}'", StrWhere, Hb_OrderNum);
            if(DbLocalSQL.ExecuteSql(strSQl)>0)
            {
                string SXMStr = string.Format("select PeoType,SalemanName,SalemanTel from OnlineOrders where OrderNum='{0}'", Hb_OrderNum);
                DataTable HB_OrderInfo = DbLocalSQL.Query(SXMStr).Tables[0];
                string Hb_DateJson = JsonHelper.DataTableToJSON(HB_OrderInfo);
                context.Response.Write(GenerClass.SetSucessJsData("修改成功", Hb_DateJson));
                return;
            }
            else
            {
                context.Response.Write(GenerClass.SetChangeErrorJsData("修改失败"));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}